package com.chinasofti.login.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.chinasofti.login.entity.User;
import com.chinasofti.login.service.LoginService;
import com.chinasofti.login.service.impl.LoginServiceImpl;
import com.chinasofti.login.vo.RsObject;

public class LoginController extends HttpServlet {
	private static Log logger = LogFactory.getLog(LoginController.class);
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		User user = new User();
		String userName = req.getParameter("userName");
		String passWord = req.getParameter("passWord");
		user.setPassWord(passWord);
		user.setUserName(userName);
		LoginService loginService = new LoginServiceImpl();
		RsObject rsObject = null;
		try {
			rsObject = loginService.checkUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (rsObject.isSuccess()) {
			logger.info("登陆成功－－user("+userName+")");
			session.setAttribute("user", rsObject.getObject());
			String login_suc = "login/login_success.jsp";
			resp.sendRedirect(login_suc);
			return;
		}
		String login_fail = "login/login_fail.jsp";
		resp.sendRedirect(login_fail);
		return;
	}
}
